package com.loan.web.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.loan.entity.*;
import com.loan.group.*;
import com.loan.service.ContractService;
import com.loan.vo.ContractArray;
import com.loan.vo.ContractEmp;
import com.loan.vo.ContractVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: ContractController
 * @Author: wYin
 * @Date: 2022/1/18 10:59
 */
@RestController
@RequestMapping("/api/contract")
public class ContractController {
    @Autowired
    private ContractService contractService;

    /**
     * 功能描述: <br>
     * 添加合同表信息
     * @Param: [contract]
     * @Return: com.loan.entity.ResponseResult<java.lang.Void>
     * @Author: wYin
     * @Date: 2022/1/18 14:56
     */
    @PostMapping("/add")
    public ResponseResult<Void> add(@RequestBody @Validated(ContractAddGroup.class) Contract contract){
        boolean flag = contractService.insert(contract);
        if(flag){
            return ResponseResult.SUCCESS;
        }
        return ResponseResult.FAIL;
    }

    /**
     * 功能描述: <br>
     * 修改合同信息：必备的条件有：合同的编号用户验证-验证业务员的合法性（只要存在当前业务员即可（只能进行业务员的更改，不能进行信息的修改）
     * 合同不能进行客户的修改（如果修改则该合同作废）
     * 签约时间不能修改
     * 可以修改收款账号
     * @Param: [contract]
     * @Return: com.loan.entity.ResponseResult<java.lang.Void>
     * @Author: wYin
     * @Date: 2022/1/18 14:58
     */
    @PostMapping("/update")
    public ResponseResult<Void> updateContractInfo(@RequestBody @Validated(ContractUpdateGroup.class) Contract contract){
        int flag = contractService.updateContractInfo(contract);
        if(flag==1){
            return new ResponseResult<>(200,"更新合同信息成功");
        }else if(flag==2){
            return new ResponseResult<>(201,"业务员与合同业务员不同，业务员更新成功，未进行合同更新");
        }
        return ResponseResult.FAIL;
    }
    
    /**
     * @Desc: 更新合同放款状态
     * @Param: [contract] : 需要传入合同的编号和操作的业务员id
     * @Return: com.loan.entity.ResponseResult<java.lang.Void>
     * @Author: wYin
     * @Date: 2022/1/19 11:38
     */
    @PostMapping("/updateLoan")
    public ResponseResult<Void> updateContractLoan(@RequestBody @Validated(ContractUpdateLoanGroup.class) Contract contract){
        boolean flag = contractService.updateContractLoan(contract);
        if(flag){
            return new ResponseResult<>(200,"已成功放款");
        }
        return new ResponseResult<>(400,"放款失败");
    }

    /**
     * @Desc: 得到单个数据合同的信息
     * @Param: [contract] [contract] 接收客户id、合同编号、合同id、业务员id或客户id
     * @Return: com.loan.entity.ResponseResult<com.loan.entity.Contract>
     * @Author: wYin
     * @Date: 2022/1/19 17:41
     */
    @PostMapping("/singleInfo")
    public ResponseResult<Contract> getSingleInfo(@RequestBody @Validated(ContractSingleSelectGroup.class)ContractVO contractVO){
        Contract singleContract = contractService.getSingleContract(contractVO);
        return new ResponseResult<>(singleContract);
    }

    /**
     * @Desc: 根据业务员ID及账户查询所有他经办的合同信息
     * @Param: [emp] 接收员工id以及员工的账号
     * @Return: com.loan.entity.ResponseResult<java.util.List<com.loan.entity.Contract>>
     * @Author: wYin
     * @Date: 2022/1/20 9:54
     */
    @PostMapping("/empGetAll")
    public ResponseResult<List<Contract>> empGetContractAll(@RequestBody Emp emp){
        List<Contract> allContract = contractService.getAllContractInfo(emp);
        return new ResponseResult<>(allContract);
    }

    /**
     * @Desc: 根据用户id及账户查询他的合同信息
     * @Param: [user]
     * @Return: com.loan.entity.ResponseResult<java.util.List<com.loan.entity.Contract>>
     * @Author: wYin
     * @Date: 2022/1/20 14:52
     */
    @PostMapping("userGetAll")
    public ResponseResult<List<Contract>> userGetContractAll(@RequestBody @Validated(UserSelectContractGroup.class) User user){
        List<Contract> list = contractService.userGetContract(user);
        return new ResponseResult<>(list);
    }

    /**
     * @Desc: 根据员工的id和账户查询未放款的合同信息
     * @Param: [contractEmp]
     * @Return: com.loan.entity.ResponseResult<java.util.List<com.loan.entity.Contract>>
     * @Author: wYin
     * @Date: 2022/1/22 10:39
     */
    @PostMapping("/selectLoanStatusIsN")
    public ResponseResult<List<Contract>> getLoanStatusIsN(@RequestBody @Validated(LoanStatusGroup.class) ContractEmp contractEmp){
        List<Contract> loanStatusIsN = contractService.getLoanStatusIsN(contractEmp);
        return new ResponseResult<>(loanStatusIsN);
    }

    @PostMapping("/batchUpdateLoan")
    public ResponseResult<Void> batchUpdateLoan(@RequestBody @Validated(ContractArrayGroup.class) List<ContractArray> contractArray){
        Boolean flag = contractService.batchUpdateLoan(contractArray);
        if(flag){
            return new ResponseResult<>(200,"批量放款成功");
        }
        return ResponseResult.FAIL;
    }



}
